/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.comp.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BaseCategoryMapper;
import com.smooth.dao.gen.BaseCitiesMapper;
import com.smooth.dao.gen.BizCompanyMapper;
import com.smooth.dao.gen.BizJobMapper;
import com.smooth.dao.gen.bean.BaseCategory;
import com.smooth.dao.gen.bean.BaseCities;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizCompanyExample;
import com.smooth.dao.gen.bean.BizJobExample;
import com.smooth.site.sx.pc.job.enums.JobStatus;

/**
 * 公司Service
 * @author 北冥
 * @version 2018-12-26
 */
@Service
@Transactional(readOnly = true)
public class BizCompanyService{

	@Autowired
	BizCompanyMapper bizCompanyMapper;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BaseCategoryMapper baseCategoryMapper;
	@Autowired
	BizJobMapper bizJobMapper;
	@Autowired
	BaseCitiesMapper baseCitiesMapper;
	public BizCompany get(String no) {
		BizCompany bizCompany = bizCompanyMapper.selectByPrimaryKey(no);
		if(bizCompany==null) {
			return null;
		}
		BizJobExample bizJobExample = new BizJobExample();
		BizJobExample.Criteria criteria = bizJobExample.createCriteria();
		criteria.andCompIdEqualTo(no);
		criteria.andJobStatusEqualTo(JobStatus.published.value);
		Long jobCount = bizJobMapper.countByExample(bizJobExample);
		bizCompany.setJobCount(jobCount.intValue());
		bizCompany.setPercent(getPercent(bizCompany));
		return bizCompany;
	}
	
	public static double getPercent(BizCompany bizCompany) {
		double sumnum = 19.00d;
		double curnum = 0.00d;
		if(bizCompany==null) {
			return 0d;
		}
		if(StringUtils.isNotBlank(bizCompany.getAccountNo())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getAccountNo())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getCompName())) {
			curnum = curnum + 1;
		}
		
		if(bizCompany.getNature()!=null) {
			curnum = curnum + 1;
		}
		
		if(bizCompany.getScale()!=null) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getCategoryId())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getProvinceId())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getCityId())) {
			curnum = curnum + 1;
		}
		if(StringUtils.isNotBlank(bizCompany.getDetail())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getCompAddress())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getCompLicensePic())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getDetail())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getContactTelNo())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getContactJob())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getContactName())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getCompLicensePic())) {
			curnum = curnum + 1;
		}
		
		
		if(StringUtils.isNotBlank(bizCompany.getCompPics())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getCompPic())) {
			curnum = curnum + 1;
		}
		
		if(StringUtils.isNotBlank(bizCompany.getZhusuPics())) {
			curnum = curnum + 1;
		}
		
//		if(bizCompany.getJiabanFee()!=null) {
//			curnum = curnum + 1;
//		}
//		if(bizCompany.getJiabanType()!=null) {
//			curnum = curnum + 1;
//		}
//		if(bizCompany.getJobTime()!=null) {
//			curnum = curnum + 1;
//		}
		return curnum/sumnum;
	}
	
	public List<BizCompany> findList(BizCompany bizCompany) {
		BizCompanyExample bizCompanyExample = new BizCompanyExample();
		BizCompanyExample.Criteria criteria = bizCompanyExample.createCriteria();
		
		return bizCompanyMapper.selectByExample(bizCompanyExample);
	}
	
	public Page<BizCompany> findPage(Page<BizCompany> page, BizCompany bizCompany) {
		BizCompanyExample bizCompanyExample = new BizCompanyExample();
		BizCompanyExample.Criteria criteria = bizCompanyExample.createCriteria();
		
		if(!StringUtils.isBlank(bizCompany.getCategoryId())) {
			criteria.andCategoryIdEqualTo(bizCompany.getCategoryId());
		}
		bizCompanyExample.setPage(page);
		
		List<BizCompany> dlist = bizCompanyMapper.selectByExample(bizCompanyExample);
		page.setList(dlist);
		page.setCount(bizCompanyMapper.countByExample(bizCompanyExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BizCompany bizCompany) {
		setCategory(bizCompany);
		bizCompany.setPercent(getPercent(bizCompany));
		if(StringUtils.isNotEmpty(bizCompany.getId())&&get(bizCompany.getId())!=null) {
			bizCompanyMapper.updateByPrimaryKeySelective(bizCompany);
		}else {
			Date date = new Date();
			bizCompany.setCreateDate(date);
			bizCompany.setId(idGenerater.genClsNo("bizCompany"));
			bizCompany.setUpdateDate(date);
			bizCompanyMapper.insertSelective(bizCompany);
		}
	}
	
	public void subJobshenyu(String compId) {
		BizCompany bizCompany = get(compId);
		Integer jobNum  = bizCompany.getJobShengyu()==null?0:bizCompany.getJobShengyu();
		
		BizCompany bizCompanyRecord = new BizCompany();
		bizCompanyRecord.setId(compId);
		bizCompanyRecord.setJobShengyu(jobNum-1);
		save(bizCompanyRecord);
	}
	
	public boolean canPubJob(String compId) {
		BizCompany bizCompany = get(compId);
		Integer jobNum  = bizCompany.getJobShengyu()==null?0:bizCompany.getJobShengyu();
		return jobNum>0;
	}
	public void setCategory(BizCompany bizCompany) {
		if(StringUtils.isNotBlank(bizCompany.getCategoryId())) {
			BaseCategory baseCategory = baseCategoryMapper.selectByPrimaryKey(bizCompany.getCategoryId());
			
			bizCompany.setCategoryName(baseCategory.getCategoryName());
		}
		if(StringUtils.isNotBlank(bizCompany.getCityId())) {
			BaseCities baseCities = baseCitiesMapper.selectByPrimaryKey(bizCompany.getCityId());
			bizCompany.setCityName(baseCities.getCityName());
		}
	}
	
	
	@Transactional(readOnly = false)
	public void delete(BizCompany bizCompany) {
		bizCompanyMapper.deleteByPrimaryKey(bizCompany.getId());
	}
	
	
	
	public BizCompany findByTelno(String telNo) {
		BizCompanyExample bizCompanyExample = new BizCompanyExample();
		BizCompanyExample.Criteria criteria = bizCompanyExample.createCriteria();
		
		criteria.andContactTelNoEqualTo(telNo);
		
		List<BizCompany> dlist = bizCompanyMapper.selectByExample(bizCompanyExample);
		if(dlist.size()>0){
			return dlist.get(0);
		}
		return null;
	}
	
}